//工厂方法 创建对象
//需要先new个对象，再赋值
/*
function createCar(sColor,iDoors,iMpg) {
    var oTempCar = new Object;
    oTempCar.color = sColor;
    oTempCar.doors = iDoors;
    oTempCar.mpg = iMpg;
    oTempCar.showColor = function() {
      alert(this.color);
    };
    return oTempCar;
  }
  
  var oCar1 = createCar("red",4,23);
  var oCar2 = createCar("blue",3,25);
  
  oCar1.showColor();		//输出 "red"
  oCar2.showColor();		//输出 "blue"
*/

//构造函数方法
//每个对象的函数都占一份内存，导致内存浪费
/*
function Car(sColor,iDoors,iMpg){
    this.color = sColor;
    this.door = iDoors;
    this.mpg = iMpg;
    this.showColor = function(){
        alert(this.color);
    }
}

var oCar1 = new Car("red",4,23);
var oCar2 = new Car("blue",3,25);

oCar1.showColor();
oCar2.showColor();
*/

//原型模式+构造函数
//缺:对象属性与函数定义分开
/*
function Car(sColor,iDoors,iMpg){
    this.color = sColor;
    this.door = iDoors;
    this.mpg = iMpg;
    this.drivers = new Array("Mike","John");
}
//使用原型赋值方法
Car.prototype.showColor = function(){
    alert(this.color);
}
//使用原型赋值的变量 全局共享，类似静态变量
//Car.prototype.drivers = new Array("Mike","John");

var oCar1 = new Car("red",4,23);
var oCar2 = new Car("blue",3,25);

oCar1.drivers.push("Vito");
alert(oCar1.drivers);
alert(oCar2.drivers);
*/


//动态原型模式
/*
function Car(sColor,iDoors,iMpg){
    this.color = sColor;
    this.door = iDoors;
    this.mpg = iMpg;
    this.drivers = new Array("Mike","John");

    //初始化方法
    if (!Car._initialized_){
        //在其中初始化方法
        //使用原型赋值方法
        Car.prototype.showColor = function(){
        alert(this.color);
        }

        //当函数初始化完后，将initialized标识，设置为true
        this._initialized_ = true;
    }

    
}

//使用原型赋值的变量 全局共享，类似静态变量
//Car.prototype.drivers = new Array("Mike","John");

var oCar1 = new Car("red",4,23);
var oCar2 = new Car("blue",3,25);

oCar1.drivers.push("Vito");
alert(oCar1.drivers);
alert(oCar2.drivers);
oCar1.showColor();
*/

//实例
//StringBuffer
/**
 * 字符串缓存对象
 */
function StringBuffer(){
    this._strings_ = new Array();
    if(!this._initialized_){
        
        /**
         * 拼接字符串
         * @param {String} str 需要拼接的字符串 
         */
        StringBuffer.prototype.append = function(str){
            this._strings_.push(str);
        }
        
        /**
         * 返回拼接后的字符串
         * @returns 拼接后的字符串
         */
        StringBuffer.prototype.toString = function(){
            return this._strings_.join("");
        }


        this._initialized_ = true;
    }
}

var stringBuffer = new StringBuffer();
stringBuffer.append("hello,");
stringBuffer.append("world");
alert(stringBuffer.toString());



